//
//  Person_LeftViewController.m
//  CarHome
//
//  Created by 王建男 on 14-9-20.
//  Copyright (c) 2014年 WJN_work@163.com. All rights reserved.
//

#import "Person_LeftViewController.h"
#import "RegulationViewController.h"
#import "SecondViewController.h"
#import "MaintenanceViewController.h"
#import "BrowseViewController.h"

@interface Person_LeftViewController ()

@end

@implementation Person_LeftViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = myColor_dark;
    [self createScrollView];
    [self createButton];
    [self createImageView];
    [self createLabel];
}

#pragma mark -
#pragma mark 创建图标

- (void)createImageView
{
    UIImageView *aImage = [[UIImageView alloc] initWithFrame:CGRectMake(60, 50, 120, 120)];
    [aImage setImage:[UIImage imageNamed:@"ctIcon.png"]];
    [_scroll addSubview:aImage];
    [aImage release];
}

#pragma mark -
#pragma mark 创建scrollview

-(void)createScrollView
{
    _scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _scroll.contentSize = CGSizeMake(0, 600);
    [self.view addSubview:_scroll];
    [_scroll release];
}

#pragma mark -
#pragma mark 创建button

- (void)createButton
{
    
    
    NSArray *arr = [[NSArray alloc] initWithObjects:@"浏览记录", @"违章查询", @"  二手车  ", @"保养优惠", @"清除缓存", nil];
    for (int i = 0; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(20, 200 + 60 * i, 200, 50)];
        button.tag = 100 + i;
        [button setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"cticon%d.png", i + 1]] forState:UIControlStateNormal];
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 60);
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_scroll addSubview:button];
    }
}

- (void)createLabel
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 530, 240, 40)];
    label.text = @"开发者：王建男 蒋寒霄 张世琦 丁昊";
    [label setFont:[UIFont systemFontOfSize:12]];
    label.textColor = [UIColor colorWithWhite:0.7 alpha:0.8];
    label.textAlignment = NSTextAlignmentCenter;
    [_scroll addSubview:label];
    [label release];
}

- (void)buttonAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    if (button.tag == 100) {
        BrowseViewController *browse = [[BrowseViewController alloc] init];
        [self presentViewController:browse animated:NO completion:nil];
        [browse release];
    } else if (button.tag == 101) {
        RegulationViewController *regulation = [[RegulationViewController alloc] init];
        [self presentViewController:regulation animated:NO completion:nil];
        [regulation release];
    } else if (button.tag == 102) {
        SecondViewController *second = [[SecondViewController alloc] init];
        [self presentViewController:second animated:NO completion:nil];
        [second release];
    } else if (button.tag == 103) {
        MaintenanceViewController *maintenance = [[MaintenanceViewController alloc] init];
        [self presentViewController:maintenance animated:NO completion:nil];
        [maintenance release];
    } else if (button.tag == 104) {
        [button setTitle:@"正在清理中..." forState:UIControlStateNormal];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *path = [paths lastObject];
        
        NSString *str = [NSString stringWithFormat:@"缓存已清除%.1fM", [self folderSizeAtPath:path]];
        
        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:path];
        for (NSString *p in files) {
            NSError *error;
            NSString *Path = [path stringByAppendingPathComponent:p];
            if ([[NSFileManager defaultManager] fileExistsAtPath:Path]) {
                [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];
            }
        }
        
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIButton *button = (UIButton *)[self.view viewWithTag:104];
    [button setTitle:@"清除缓存" forState:UIControlStateNormal];
}


- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
//遍历文件夹获得文件夹大小，返回多少M
- (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
